package com.hdoit.ares.web.cbase;

import java.io.FileNotFoundException;

import org.springframework.util.ResourceUtils;



import com.tennetcn.common.message.ConfigurationBase;
import com.tennetcn.component.wechat.config.WechatConfigModel;

public class AppWechatConfiguration extends ConfigurationBase{
	public AppWechatConfiguration(String path){
		super(path);
	}
	
	private static AppWechatConfiguration configuration;
	
	public WechatConfigModel wechatConfigModel;
	
	public static AppWechatConfiguration getInstance(){
		if(configuration==null){
    		configuration=new AppWechatConfiguration("appWechat.properties");
    		configuration.resolveConfigModel();
    	}
    	return configuration;
	}
	
	public WechatConfigModel resolveConfigModel(){
			if(wechatConfigModel!=null){
				return wechatConfigModel;
			}
			wechatConfigModel=new WechatConfigModel();
			
			wechatConfigModel.setAppId(configuration.getValue("wechat.appId"));
			wechatConfigModel.setSecret(configuration.getValue("wechat.secret"));
			wechatConfigModel.setToken(configuration.getValue("wechat.token"));
			wechatConfigModel.setAesKey(configuration.getValue("wechat.aesKey"));
			wechatConfigModel.setPublicUrl(configuration.getValue("publicUrl"));
			
			wechatConfigModel.setMchId(configuration.getValue("wechat.pay.mchId"));
			wechatConfigModel.setMchKey(configuration.getValue("wechat.pay.mchKey"));
			wechatConfigModel.setSubAppId(configuration.getValue("wechat.pay.subAppId"));
			wechatConfigModel.setSubMchId(configuration.getValue("wechat.pay.subMchId"));
			
			String keypath=configuration.getValue("wechat.pay.keyPath");
			if(keypath.indexOf("classpath:")>=0){
				try {
					String classpath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
					keypath=keypath.replace("classpath:", classpath);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
			wechatConfigModel.setKeyPath(keypath);
			
			wechatConfigModel.setOrgCode(configuration.getValue("wechat.pay.orgCode"));
			wechatConfigModel.setSpbillCreateIp(configuration.getValue("wechat.pay.spbillCreateIp"));
			wechatConfigModel.setNotifyURL(configuration.getValue("wechat.pay.notifyURL"));
			wechatConfigModel.setTradeType(configuration.getValue("wechat.pay.tradeType"));
			
			return wechatConfigModel;
		}
}
